<?php

/*
 * 企业俱乐部项目Action
 */
class MassAction extends Action {
	
	//获得标签
	public function getMassMsg($order="sendtime", $orderBy="desc", $start = 0, $limit = 25){	
	$retVal = Session::init();
		if (!$retVal) {
			Output::instance()->resultCode(code_access_denied);
			return;
		}
		
		$model = D('module_mass_havesend');
		$arr  = $model->order("$order $orderBy")->limit("$start, $limit")->select();	
		if ($arr) {
			$count = $model->count();
			Output::instance()->objArray($arr, $count);
		} else {
			Output::instance()->resultCode(code_success);
		}
	}
	
	public function getAccessTag(){
		$retVal = Session::init();
		if (!$retVal) {
			Output::instance()->resultCode(code_access_denied);
			return;
		}
		
		$model = D('access_tag');
		$arr  = $model->select();	
		//$model = D('user_access_tag');	
		//$arr  = $model->alias('at')->join("user_access_tag ON  user_access_tag.tagName=at.tagName")->select();		
		//$result=WeixinApi::delmass($msg_id);
		Output::instance()->object($arr);
		
	
	}
	//ihziluoh 添加通过标签id获取用户列表
	public function AccessTagsUser($AccessTagId,$order="createTime", $orderBy="desc", $start = 0, $limit = 25){	
		$retVal = Session::init();
		if (!$retVal) {
			Output::instance()->resultCode(code_access_denied);
			return;
		}
		$cid = Session::$user["customerId"];
		//获取发送用户 
		$model = M('user_access_tag');
		$access_tagmodel = D('access_tag');		
		$where['accesstagid']=$AccessTagId;		
		$arr = $model->alias('accessuser')
						->join("wx_".$cid."_user_weixin_info as userinfo  on userinfo.openid=accessuser.fromUserName")
						->where($where)
						->Distinct(true)
						->field('nickName,headimgurl')
						->order("$order $orderBy")
						->limit("$start, $limit")
						->select();
		if ($arr) {
			$count = $model->where($where)->count("DISTINCT fromUserName");
			Output::instance()->objArray($arr, $count);
		} else {
			Output::instance()->resultCode(code_mass_accesstag_no_user);
			return;
		}		
	}
	
	/*$touser 群发多用户
	 * $mpnews 用于设定即将发送的图文消息（固定不需要传参）
	 * $media_id 用于群发的图文消息的media_id
	 * $msgtype 群发的消息类型，图文消息为mpnews，文本消息为text，语音为voice，音乐为music，图片为image，视频为video
	 * $title 消息的标题
	 * $description 	消息的描述
	 * $thumb_media_id 视频缩略图的媒体ID
	 * $text 文本消息内容
	 * $labels 标签 逗号隔开
	*/
	
	public function massSend($AccessTags, $msgtype, $title='', $media_id='', $description='', $thumb_media_id='',$text=''){	
		$retVal = Session::init();
		if (!$retVal) {
			Output::instance()->resultCode(code_access_denied);
			return;
		}
		//获取发送用户 
		$model = M('user_access_tag');
		$access_tagmodel = D('access_tag');		
		$where['accesstagid']=array('in',$AccessTags);		
		$arr = $model->where($where)->Distinct(true)->field('fromUserName')->select();
		Log::write("群发sql".$model->getlastsql());
		if (!$arr) {
			Output::instance()->resultCode(code_mass_accesstag_no_user);
			return;
		}
		/*$i=0;
		foreach($arr as $value){
			if($i==0){
			$touser=$value['fromUserName'];
			}else{
			$touser.=','.$value['fromUserName'];
			}
			$i++;
		}
		*/
	
		//获取标签名称
		$accesswhere['id']=array('in',$AccessTags);	
		$tagarr = $access_tagmodel->field('tagName')->where($accesswhere)->select();
// 		log::write("$AccessTags:".$AccessTags.'   accesstags name;'.$access_tagmodel->getLastSql());
		$i = 0;
		foreach($tagarr as $value){
			if($i==0){
				$tagNames=$value['tagName'];
			}else{
				$tagNames.=','.$value['tagName'];
			}
			$i++;
		}
		
		$usercount=Ceil(count($arr)/9999);		
		for($i=0;$i<=$usercount-1;$i++){
			//log::write($i);
			$arruser=array_slice($arr,$i*9999,9999);
			//log::write(count($arruser));
			foreach($arruser as $value){
				$touser.=$value['fromUserName'].',';
			}
			$touser = substr($touser,0,strlen($touser)-1);
			// 						log::write($touser);
			$result = WeixinApi::mass($touser, $media_id,$msgtype, $title, $description, $thumb_media_id,$text);
			if (!$result) {
				return;
			}
			
			$mass = D('module_mass_havesend');
			$data['labels'] = $tagNames;	 //暂无
			$data['openids'] = $touser;
			$data['msgtype'] = $msgtype;
			log::write('msg_id'.$result->msg_id);
			$data['sendtime'] = date('Y-m-d H:i:s');
			$data['msg_id'] = $result->msg_id;	 //暂无
			$data['msgtype'] = $msgtype;
			switch ($msgtype) {
				case mpnews:
					//图文
					$data['sendcontent'] = $text;
					$data['type'] = 'mpnews';							//暂无
					//$msg=json_encode($msg);
					break;
				case text:
					//文本
					$data['sendcontent'] = $text;
					$data['type'] = 'text';
					break;
					/*	case voice:
					 $data['voice']['media_id'] = $media_id;
					$data['title'] = $title;
					$data['description'] = $description;
					//语音
					break;
					case image:
					$data['image']['media_id'] = $media_id;
					$data['title'] = $title;
					$data['description'] = $description;
					//图片
					break;
					case video:
					$data['video']['media_id'] = $media_id;
					$data['video']['title'] = $title;
					$data['description']['title'] = $description;
					//视频
					break;	*/
				default:
					throw_exception('不存在的群发类型');
			}
			$save = $mass->add($data);
			
			$touser='';
		}
		Output::instance()->object($result);
	//	WeixinApi::mass('群发用户','图文消息的media_id','消息类型','$title','$description','视频缩略图的媒体ID','$text 文本消息内容');
	}	
	//上传素材
	public function massUploadMaterial($articles){
			//	echo 'asdf';
		$retVal = Session::init();
		if (!$retVal) {
			Output::instance()->resultCode(code_access_denied);
			return;
		}
		$result=WeixinApi::uploadMaterial($articles);
		Output::instance()->object($result);
	//	WeixinApi::mass('群发用户','图文消息的media_id','消息类型','$title','$description','视频缩略图的媒体ID','$text 文本消息内容');
	}
	//删除已发送的群发消息 微信端
	public function delhavesend($msg_id){
		$retVal = Session::init();
		if (!$retVal) {
			Output::instance()->resultCode(code_access_denied);
			return;
		}
		$result = WeixinApi::delmass($msg_id);
		if (!$result) {
			return;
		}
		$del = D('module_mass_havesend');	
		$where['msg_id']=$msg_id;
		$data['isdel']=1;		
		$del->where($where)->save($data);
		log::write('delsql:'.$del->getLastSql());
		
		Output::instance()->resultCode();
	}
	
	public function uploadMedia($type,$filepath){
		log::write('Enter the uploadMedia  '.'  type:'.$type.'  filepath:'.$filepath);
		$retVal = Session::init();
		if (!$retVal) {
			Output::instance()->resultCode(code_access_denied);
			return;
		}	
		$result=WeixinApi::uploadMedia($type,$filepath);
		if (!$result) {
			return;
		}
		Output::instance()->object($result);
	//	WeixinApi::mass('群发用户','图文消息的media_id','消息类型','$title','$description','视频缩略图的媒体ID','$text 文本消息内容');
	}
	
	
}